package com.check.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.check.pojo.Dictionary;
import com.check.pojo.Opr;
import com.check.pojo.OprTemp;
import com.check.pojo.Org;
import com.check.pojo.PageObject;
import com.check.pojo.WorkRole;
import com.check.service.AgencyService;
import com.check.service.DictionaryService;
import com.check.service.PersonService;
import com.check.service.PersonTempService;
import com.check.service.WorkRoleService;
import com.check.util.StringUtil;

@Controller
public class PersonTempController {
	
	@Autowired
	@Qualifier("personTempService")
	private PersonTempService personTempService;
	
	@Autowired
	@Qualifier("personService")
	private PersonService personService;
	
	@Autowired
	@Qualifier("agencyService")
	private AgencyService agencyService;
	
	@Autowired
	@Qualifier("dictionaryService")
	private DictionaryService dictionaryService;
	
	@Autowired
	@Qualifier("workRoleService")
	private WorkRoleService workRoleService;
	
	@RequestMapping("findPersonTemp.do")
	public ModelAndView findPersonTemp(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("pageObject")PageObject pageObject){
		String oprId = request.getParameter("oprId");
		List<OprTemp> tempList = personTempService.findAllOprTemp(oprId);
		return new ModelAndView("page/person/personTemp")
				.addObject("path",request.getContextPath())
				.addObject("tempList",tempList)
				.addObject("oprId",oprId);
	}
	
	@RequestMapping("toAddPersonTemp.do")
	public ModelAndView toAddPersonTemp(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("pageObject")PageObject pageObject){
		String oprId = request.getParameter("oprid");
		Opr opr = personService.findByOprId(oprId);
		List<Dictionary> type1List = dictionaryService.findByCode("type1");
		List<Dictionary> type2List = dictionaryService.findByCode("type2");
		List<WorkRole> workRoleList = workRoleService.findAllWorkRoleByType("1");
		return new ModelAndView("page/person/personTempAdd")
				.addObject("path",request.getContextPath())
				.addObject("opr",opr)
				.addObject("type1List",type1List)
				.addObject("type2List",type2List)
				.addObject("workRoleList",workRoleList);
	}
	
	
	@RequestMapping("personTempManagerDept.do")
	public ModelAndView personTempManagerDept(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("pageObject")PageObject pageObject){
		String oprId = request.getParameter("oprId");
		String orgId = request.getParameter("orgId");
		OprTemp oprTemp=new OprTemp();
		oprTemp.setOprId(oprId);
		oprTemp.setOrgId(orgId);
		OprTemp findOprTemp=null;
		if(oprId!=null && orgId!=null){
			findOprTemp=personTempService.findTempByOprIdAndOrgId(oprTemp);
		}
		String checkOrgIds="";
		if(findOprTemp!=null){
			checkOrgIds=findOprTemp.getCheckOrgId();
		}else{
			checkOrgIds="";
		}
		List<Org> orgList = agencyService.findAllOrgByType("0");
		return new ModelAndView("page/person/personDept")
				.addObject("path",request.getContextPath())
				.addObject("orgList",orgList)
				.addObject("checkOrgIds",checkOrgIds);
				
	}
	
	@RequestMapping("personTempAdd.do")
	public void personTempAdd(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("oprTemp")OprTemp oprTemp){
		PrintWriter out = null;
		String flag = "0";
		
		try {
			String o_id = request.getParameter("orgId");
			Org org = agencyService.findOrgById(o_id);
			if(org!=null){
				
				if("0".equals(org.getO_type())){
					oprTemp.setOrgId(o_id);
					oprTemp.setDeptId(o_id);
					oprTemp.setRoomId("");
				}else{
					oprTemp.setOrgId(o_id);
					oprTemp.setDeptId(org.getO_upid());
					oprTemp.setRoomId(o_id);
				}
			}else{
				oprTemp.setOrgId(o_id);
				oprTemp.setDeptId("");
				oprTemp.setRoomId("");
			}
			response.setContentType("text/html;charset=gbk");
			out = response.getWriter();
			OprTemp findOprTemp = personTempService.findTempByOprIdAndOrgId(oprTemp);
			if(findOprTemp==null){
				if(personTempService.insertOprTemp(oprTemp)){
					flag = "1";
				}
			}else{
				flag="2";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
	}
	
	@RequestMapping("toUpdatePersonTemp.do")
	public ModelAndView toUpdatePersonTemp(HttpServletRequest request,HttpServletResponse response){
		String orgId = request.getParameter("orgId");
		String oprId = request.getParameter("oprId");
		OprTemp oprTemp=new OprTemp();
		oprTemp.setOrgId(orgId);
		oprTemp.setOprId(oprId);
		oprTemp = personTempService.findTempByOprIdAndOrgId(oprTemp);
		List<Dictionary> type1List = dictionaryService.findByCode("type1");
		List<Dictionary> type2List = dictionaryService.findByCode("type2");
		List<WorkRole> workRoleList = workRoleService.findAllWorkRoleByType("1");
		
		return new ModelAndView("page/person/personTempUpdate")
					.addObject("path",request.getContextPath())
					.addObject("opr",oprTemp)
					.addObject("type1List",type1List)
					.addObject("type2List",type2List)
					.addObject("workRoleList",workRoleList);
		
	}
	
	@RequestMapping("personTempUpdate.do")
	public void personTempUpdate(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("oprTemp")OprTemp oprTemp){
		PrintWriter out = null;
		String flag = "0";
		
		try {
			response.setContentType("text/html;charset=gbk");
			out = response.getWriter();
			if(personTempService.updateOprTemp(oprTemp)){
				flag = "1";
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
	}
	
	@RequestMapping("personTempDelete.do")
	public void personTempDelete(HttpServletRequest request,HttpServletResponse response){
		PrintWriter out = null;
		String flag = "0";
		String orgId = request.getParameter("orgId");
		String oprId = request.getParameter("oprId");
		try {
			response.setContentType("text/html;charset=gbk");
			out=response.getWriter();
			OprTemp oprTemp=new OprTemp();
			oprTemp.setOprId(oprId);
			oprTemp.setOrgId(orgId);
			if(personTempService.deleteOprTemp(oprTemp)){
				flag = "1";
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
	}
	
}
